package biz.datalk.industrialland.common.advice;

import biz.datalk.industrialland.common.advice.cfg.DatalkCryptoCfgs;
import biz.datalk.industrialland.common.advice.mapper.IAppEncryptSecurityKeyMapper;
import biz.datalk.industrialland.common.component.AppEncryptSecurityKeyComponent;
import biz.datalk.industrialland.common.util.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author tarofang@163.com
 * @date 2022-10-10
 */
@Configuration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@EnableConfigurationProperties(DatalkCryptoCfgs.class)
public class RequestDecryptResponseEncryptConf {
    private static final Logger log = LoggerFactory.getLogger(RequestDecryptResponseEncryptConf.class);


    @Bean
    @ConditionalOnMissingBean
    // @ConditionalOnProperty(name = {"datalk.crypto.enable", "datalk.crypto.check-signature-interceptor"}, havingValue = "true", matchIfMissing = true)
    public CheckSignatureInterceptor checkSignatureInterceptor(RedisUtil redisUtil, DatalkCryptoCfgs datalkCryptoCfgs) {
        log.debug("checkSignatureInterceptor ====================================== into");
        try {
            return new CheckSignatureInterceptor(redisUtil, datalkCryptoCfgs);
        } finally {
            log.debug("checkSignatureInterceptor ====================================== out");
        }
    }

    @Bean
    @ConditionalOnMissingBean
    // @ConditionalOnProperty(name = {"datalk.crypto.enable", "datalk.crypto.app-encrypt-security-key-component"}, havingValue = "true", matchIfMissing = true)
    public AppEncryptSecurityKeyComponent appEncryptSecurityKeyComponent(
            RedisUtil redisUtil, IAppEncryptSecurityKeyMapper appEncryptSecurityKeyMapper, DatalkCryptoCfgs datalkCryptoCfgs) {
        log.debug("appEncryptSecurityKeyComponent ====================================== into");
        try {
            return new AppEncryptSecurityKeyComponent(redisUtil, appEncryptSecurityKeyMapper, datalkCryptoCfgs);

        } finally {
            log.debug("appEncryptSecurityKeyComponent ====================================== out");
        }
    }

}
